/**
 * 子弹
 * by wyy
 */
class bullet extends GameObject implements eui.UIComponent {
    private data = {
        mass: 1,
        position: [-100, -100],
        radius: 0.4
    };
    public phyBody: p2.Body;
    private life: number = 15;
    public maxLife: number = 15;
    public type: string = "bullet";

    constructor({ mass = 1, position = [-100, -100], radius = 0.2 }) {
        super();
        this.skinName = "resource/eui_skins/game_skins/bulletSkin.exml";
        this.data.mass = mass;
        this.data.position = position;
        this.data.radius = radius;
    }
    childrenCreated() {
    }
    //创建的时候初始化
    firstCreate() {
        let phy = physicsUtil.getInstance();
        let data = this.data;
        let positionX = data.position[0] / phy.phyFactor;
        let positionY = (egret.MainContext.instance.stage.stageHeight - data.position[1]) / phy.phyFactor;
        let phyShape = new p2.Circle({ radius: data.radius, color: 0xffffff });
        phyShape.material = phy.bulletMat;
        this.phyBody =
            new p2.Body({ mass: data.mass, position: [positionX, positionY] });
        this.phyBody.addShape(phyShape);
        this.phyBody.displays = [this];
        this.anchorOffsetX = this.width / 2;
        this.anchorOffsetY = this.height / 2;
        this.phyBody.damping = 0;
        this.phyBody.angularDamping = 0;
    }
    addToStage() {
        let p = gameScene.getInstance().player;
        let startPos = p.localToGlobal(p.player.x, p.player.y);
        this.setPos(startPos.x, startPos.y);
    }
    onRemove() {

    }
    setPos(x, y) {
        let [rX, rY] = physicsUtil.getInstance().posEgretToP2(x, y);
        this.phyBody.position = [rX, rY];
        this.life = this.maxLife;
    }
    //击中后执行
    hited(t) {
        //扣减次数
        this.life -= 1;
        if (this.life <= 0) {
            this.setPos(-100, -100);
            this.x = -100;
            this.y = -100;
            bulletPool.getInstance().recycleBullet(this);
            physicsUtil.getInstance().phyWorld.removeBody(this.phyBody);
        }
        //扣减砖块生命
        if (t.type == "brick") {
            t.hited();
        }
    }

}